//
//  UITableViewCell+us.m
//  us
//
//  Created by karry on 2025/4/8.
//

#import "UITableViewCell+us.h"

@implementation UITableViewCell (us)

+ (instancetype)us_defaultCell{
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellId"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.textLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    
    cell.backgroundColor = UIColor.clearColor;
    
    cell.tintColor = UIColor.labelColor;
    
    return cell;
}

- (void)us_setAccessoryViewImage:(UIImage *)image{
    
    UIImageView *customAccessoryView = [[UIImageView alloc] initWithImage:[image imageWithTintColor:[UIColor lightGrayColor]]];
    customAccessoryView.frame = CGRectMake(0, 0, 22, 22); // 根据需要设置大小
    self.accessoryView = customAccessoryView;
}

- (void)us_setDefaultArrow{
    [self us_setAccessoryViewImage:[UIImage imageNamed:@"arrow_0"]];
}

- (void)us_setAccessorySwitchisSelected:(BOOL)isSelected
                                        action:(void(^)(BOOL selected))toggleAction {
    
    UIButton *switchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [switchBtn setImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal];
    [switchBtn setImage:[UIImage imageNamed:@"on"] forState:UIControlStateSelected];
    [switchBtn sizeToFit];
    
    switchBtn.selected = isSelected;
    
    [switchBtn addTarget:self action:@selector(us_accessoryButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    // 存储回调（利用关联对象）
    objc_setAssociatedObject(self, @selector(us_accessoryButtonTapped:), toggleAction, OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, @"us_switch_button", switchBtn, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    self.accessoryView = switchBtn;
}

- (void)us_setAccessorySwitchIsSelected:(BOOL)isSelected
                            allowToggle:(BOOL)allowToggle
                                 action:(void(^)(BOOL selected))toggleAction
                         disabledAction:(void(^)(void))disabledAction {
    
    UIButton *switchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [switchBtn setImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal];
    [switchBtn setImage:[UIImage imageNamed:@"on"] forState:UIControlStateSelected];
    [switchBtn sizeToFit];
    
    switchBtn.selected = isSelected;
    
    // 设置点击事件
    [switchBtn addTarget:self action:@selector(us_accessoryButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    // 存储回调（利用关联对象）
    objc_setAssociatedObject(self, @selector(us_accessoryButtonTapped:), toggleAction, OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, @"us_switch_button", switchBtn, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, @"us_switch_allow_toggle", @(allowToggle), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, @"us_switch_disabled_action", disabledAction, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
    self.accessoryView = switchBtn;
}

- (void)us_accessoryButtonTapped:(UIButton *)sender {
    NSNumber *allowToggleObj = objc_getAssociatedObject(self, @"us_switch_allow_toggle");
    BOOL allowToggle = allowToggleObj ? [allowToggleObj boolValue] : YES; // 默认 YES

    if (allowToggle) {
        sender.selected = !sender.selected;
        
        if (sender.selected) {
            [USFeedbackTool impactHeavy];
        } else {
            [USFeedbackTool selectionChanged];
        }
        
        void (^actionBlock)(BOOL) = objc_getAssociatedObject(self, _cmd);
        if (actionBlock) {
            actionBlock(sender.selected);
        }
    } else {
        // 不允许切换时调用的回调
        void (^disabledAction)(void) = objc_getAssociatedObject(self, @"us_switch_disabled_action");
        if (disabledAction) {
            disabledAction();
        }
    }
}

//- (void)us_accessoryButtonTapped:(UIButton *)sender {
//    sender.selected = !sender.selected;
//    
//    if (sender.selected) {
//        [USFeedbackTool impactHeavy];
//    }else{
//        [USFeedbackTool selectionChanged];
//    }
//    
//    void (^actionBlock)(BOOL) = objc_getAssociatedObject(self, _cmd);
//    if (actionBlock) {
//        actionBlock(sender.selected);
//    }
//}

+ (CGFloat)us_defaultCellRowHeight{
    return 55;
}

@end
